package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BaseBookMapper;
import com.koron.css2.baseConfig.mapper.BusinessAbodeMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.queryBean.BaseBookQueryBean;
import com.koron.css2.baseConfig.vo.BaseBookListVO;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.SelectBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BaseBookList implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			BaseBookQueryBean bean = JsonUtils.objectToPojo(req.getData(), BaseBookQueryBean.class);
			bean.setSortStr(StringUtils.isBlank(bean.getSortField()) || bean.getSortOrder() == null ?
					null : (bean.getSortField() + " " + (bean.getSortOrder() == 1 ? "ASC" : "DESC")));
			BaseBookMapper mapper = factory.getMapper(BaseBookMapper.class);
			if (bean.getBusinessArea() !=null && !"".equals(bean.getBusinessArea())){
				BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
				List<String> areaIdList = this.queryChildrenArea(bean.getBusinessArea(),userInfo.getCurWaterCode(),businessAreaMapper);
				bean.setBusinessAreaList(areaIdList);
			}
			if (bean.getPage() != -1){
				PageHelper.startPage(bean.getPage(), bean.getPageCount());
			}
			List<BaseBookListVO> list = mapper.select(bean);
			BusinessAbodeMapper abodemapper = factory.getMapper(BusinessAbodeMapper.class,"_default");
			List<SelectVO> Abodes = abodemapper.businessAbodeSelect(userInfo.getCurWaterCode());
			Map<String, String> RTP = Tools.mapDicByCode(factory, "RTP");
			for (BaseBookListVO vo : list) {
				for (SelectVO abode : Abodes) {
					if(abode.getId().equals(vo.getBusinessAbode())) {
						vo.setBusinessAbodeName(abode.getName());
					}
				}
				vo.setMeterReadingCycleName(RTP.get(vo.getMeterReadingCycle()));
				if(vo.getStatus() == 1) {
					vo.setStatusName("启用");
				}else {
					vo.setStatusName("停用");
				}
				if (!StringUtils.isEmpty(vo.getBusinessArea())) {
					BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
					vo.setBusinessAreaName(businessAreaMapper.findBusinessAreaById(vo.getBusinessArea(), userInfo.getCurWaterCode()));
				}
			}
			info.setData(new PageInfo<>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("操作失败");
			logger.error("操作失败", e);
		}
		return info;
	}

	private List<String> queryChildrenArea(String parentId,String groupCode,BusinessAreaMapper mapper){
		List<SelectBean> list = mapper.findBusinessAreaByParentId(parentId,groupCode);
		List<String> result = new ArrayList<>();
		if (list != null && list.size()>0){
			for (SelectBean selectBean : list){
				List<String> sub = queryChildrenArea(selectBean.getId(),groupCode,mapper);
				result.addAll(sub);
			}
		}else {
			result.add(parentId);
		}
		//result.addAll(list.stream().map(SelectBean::getCode).collect(Collectors.toList()));

		return result;
	}

}
